<template>
  <view class="page">
    <!-- 编辑 -->
    <view class="article-edit">
      <view class="edit">
        <text @click="clearAll">清空</text>
      </view>
      <view class="edit" @click="isEdit = !isEdit">
        <text>{{isEdit?'完成':'编辑'}}</text>
      </view>
    </view>
    <!-- 记录列表 -->
    <view class="record-data" >
      <view v-if="historyInfo" class="record-list" v-for="(item,index) in historyInfo" :key="index">
        <view class="record-date">
          <text>{{item.addTime}}</text>
        </view>
        <view class="goods-list">
          <view class="list" v-for="(value,idx) in item.goodsList" :key="idx">
            <view class="check" :style="isEdit?'display: flex':'display: none'">
              <text @click="checked(value.id,index,idx)"
                :class="!checkbuttonarr[index][idx].checkbutton?'iconfont icon-check':'iconfont icon-checked'"></text>
            </view>
            <view class="thumb">
              <image :src="'/static/img/goods_thumb_0'+(index+1)+'.png'" mode=""></image>
            </view>
            <view class="item">
              <view class="title">
                <text class="two-omit">{{value.goodName}}</text>
              </view>
              <view class="price-more">
                <view class="price">￥{{value.price}}</view>
              </view>
              <view class="goods-btn">
                <view class="btn">
                  <text>看相似</text>
                </view>
                <view class="cart">
                  <text class="iconfont icon-cart"></text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
      <view v-else>暂无历史记录</view>
    </view>
    <view class="footer-btn" :style="isEdit?'display: flex':'display: none'">
      <view @click="OnDeHistory()" class="btn">删除</view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        isEdit: false,
        historyInfo: '',
        idList: [],
        checkbuttonarr: [
          
        ]
      };
    },
    onShow() {
      uni.request({
        data: {
          userId: this.$queue.getData("id")
        },
        method: 'POST',
        header: {
          "content-type": 'application/x-www-form-urlencoded'
        },
        url: "http://localhost:8089/cgoAdmin/getHistory",
        success: (res) => {
          this.historyInfo = res.data.result;
          console.log(this.historyInfo)
          for (var i = 0; i < this.historyInfo.length; i++) {
            var checkarr=[];
            for (var j = 0; j < this.historyInfo[i].goodsList.length; j++) {
              var checked = {
                'checkbutton': false
              };
              checkarr.push(checked)
            }
      
            this.checkbuttonarr.push(checkarr);
          }
          console.log(this.checkbuttonarr)
        },
      })
    },
    methods: {
      checked: function(id, index,idx) {
        if (!this.checkbuttonarr[index][idx].checkbutton) {
          this.idList.push(id);
          this.checkbuttonarr[index][idx].checkbutton = true;
        } else {
          this.idList.splice(this.idList.indexOf(id), 1);
          this.checkbuttonarr[index][idx].checkbutton = false;
        }
      },
      clearAll(){
        uni.request({
          url: 'http://localhost:8089/cgoAdmin/clearAllHistory',
          data: {
            userId: this.$queue.getData("id")
          },
          method: 'POST',
          header: {
            "content-type": 'application/x-www-form-urlencoded'
          },
          success: (res) => {
            if (res.data.success) {
              this.$queue.showToast("已清除历史");
              setTimeout(function() {
                uni.redirectTo({
                  url: "/pages/BrowsingHistory/BrowsingHistory",
                })
              }, 1000)
        
        
            } else {
              if (res.data.message != null)
                this.$queue.showToast(res.data.message);
            }
          }
        })
      },
      OnDeHistory() {
        uni.request({
          url: 'http://localhost:8089/cgoAdmin/delHistory',
          data: {
            ids: this.idList
          },
          method: 'POST',
          header: {
            "content-type": 'application/x-www-form-urlencoded'
          },
          success: (res) => {
            if (res.data.success) {
              this.$queue.showToast("已清除历史");
              setTimeout(function() {
                uni.redirectTo({
                  url: "/pages/BrowsingHistory/BrowsingHistory",
                })
              }, 1000)


            } else {
              if (res.data.message != null)
                this.$queue.showToast(res.data.message);
            }
          }
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  @import 'BrowsingHistory.scss';
</style>